const Stack = require('./stack')

function StackQueue() {
  const stack1 = new Stack()
  const stack2 = new Stack()

  this.enqueue = function (item) {
    stack1.push(item)
  }

  this.dequeue = function () {
    while (!stack1.isEmpty()) {
      stack2.push(stack1.pop())
    }
    return stack2.pop()
  }

  this.head = function () {
    if (stack1.isEmpty() && stack2.isEmpty()) return null
    if (stack2.isEmpty() && !stack1.isEmpty()) {
      while (!stack1.isEmpty()) {
        stack2.push(stack1.pop())
      }
    }
    return stack2.top()
  }
}

const stackQueue = new StackQueue()

stackQueue.enqueue(2)
stackQueue.enqueue(3)
console.log(stackQueue.head())
stackQueue.dequeue()
console.log(stackQueue.head())
stackQueue.dequeue()
console.log(stackQueue.head())
stackQueue.enqueue(100)
console.log(stackQueue.head())
